﻿///////////////////////////////////////////////////////////////////////////////
//
// This file is part of the BassDrop project
//   https://code.google.com/p/bass-drop/
//
//
// Creator:	Gergely Krajcsovszki (krajcsovszkig@hotmail.com)
// Date:	2013-07-24 19:00
//
//
// Copyright © 2013-2014. Gergely Krajcsovszki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BassDrop.Core;
using BassDrop.Core.Common.Localization;
using BassDrop.DesktopClient.Properties;
using BassDrop.DesktopClient.UI.Views;
using BassDrop.DesktopClient.Utilities;

namespace BassDrop.DesktopClient
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		/// <summary>
		/// The currently selected content view panel
		/// </summary>
		private ContentViewBase m_selectedContentView;
		/// <summary>
		/// The currently selected content view panel
		/// </summary>
		private ContentViewBase SelectedContentView
		{
			get
			{
				return m_selectedContentView;
			}

			set
			{
				if ( value == null )
				{
					throw new ArgumentNullException( "BassDrop.DesktopClient.MainWindow.SelectedContentView" );
				}

				m_selectedContentView = value;

				UpdateContent();
			}
		}

		/// <summary>
		/// The instance of the "Now Playing" view
		/// </summary>
		private NowPlayingView m_nowPlayingView;
		/// <summary>
		/// The instance of the "Media Library" view
		/// </summary>
		private MediaLibraryView m_mediaLibraryView;
		/// <summary>
		/// The instance of the "Playlists" view
		/// </summary>
		private PlaylistsView m_playlistsView;
		/// <summary>
		/// The instance of the "Clouds" view
		/// </summary>
		private CloudsView m_cloudsView;
		/// <summary>
		/// The instance of the "Settings" view
		/// </summary>
		private SettingsView m_settingsView;
		/// <summary>
		/// The splash screen to close on startup
		/// </summary>
		private SplashScreen m_splashScreen;

		/// <summary>
		/// Constructs a new MainWindow
		/// </summary>
		public MainWindow()
		{
			m_nowPlayingView = new NowPlayingView();
			m_mediaLibraryView = new MediaLibraryView();
			m_playlistsView = new PlaylistsView();
			m_cloudsView = new CloudsView();
			m_settingsView = new SettingsView();

			InitializeComponent();

			UpdateContent();
		}

		/// <summary>
		/// Constructs a new MainWindow
		/// </summary>
		/// <param name="splashScreen">The splash screen to close on startup</param>
		public MainWindow( SplashScreen splashScreen )
			: this()
		{
			m_splashScreen = splashScreen;
		}

		/// <summary>
		/// Updates the content panel based on the SelectedContentView property
		/// </summary>
		private void UpdateContent()
		{
			if ( GridContentPanel == null )
			{
				return;
			}

			GridContentPanel.Children.Clear();
			GridContentPanel.Children.Add( SelectedContentView );
		}

		/// <summary>
		/// Show the settings for the passed item.
		/// </summary>
		/// <param name="item"></param>
		public void ShowSettingsFor( object item )
		{
			m_settingsView.SelectViewForItem( item );
			RadioButtonSettings.IsChecked = true;
		}

		#region Event handlers

		private async void Window_Loaded( object sender, RoutedEventArgs e )
		{
			if ( m_splashScreen != null )
			{
				m_splashScreen.Close( TimeSpan.Zero );
			}

			foreach ( var ex in App.StartupExceptions )
			{
				await ex.Prompt( this );
			}
		}

		private async void Window_Closed( object sender, EventArgs e )
		{
			Exception error = null;

			try
			{
				await App.Instance.Close();
			}
			catch ( Exception ex )
			{
				error = ex;
			}

			if ( error != null )
			{
				await error.Prompt( this );
			}
		}

		private void RadioButtonNowPlaying_Checked( object sender, RoutedEventArgs e )
		{
			SelectedContentView = m_nowPlayingView;
		}

		private void RadioButtonMediaLibrary_Checked( object sender, RoutedEventArgs e )
		{
			SelectedContentView = m_mediaLibraryView;
		}

		private void RadioButtonPlaylists_Checked( object sender, RoutedEventArgs e )
		{
			SelectedContentView = m_playlistsView;
		}

		private void RadioButtonClouds_Checked( object sender, RoutedEventArgs e )
		{
			SelectedContentView = m_cloudsView;
		}

		private void RadioButtonSettings_Checked( object sender, RoutedEventArgs e )
		{
			SelectedContentView = m_settingsView;
		}

		#endregion Event handlers
	}
}
